//
//  DeviceConnectCheckView.m
//  sentorise
//
//  Created by admin on 2025/8/25.
//

#import "DeviceConnectCheckView.h"
#import "GlobalDefine.h"
#import "ProgressHUD.h"
#import "BlutoothGroupModel.h"
#import "HelpCenterViewController.h"

@interface DeviceConnectCheckView()

@property (nonatomic, strong) NSString *mac;
@property (nonatomic, strong) NSString *sn;
// 0搜索连接 1扫码连接
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, assign) BOOL isConnected;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentH;
@property (weak, nonatomic) IBOutlet UIView *connectView;
@property (weak, nonatomic) IBOutlet UILabel *bleNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIView *resultView;
@property (weak, nonatomic) IBOutlet UIView *scanErrorView;

@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
@property (weak, nonatomic) IBOutlet UIImageView *statusIV;
@property (weak, nonatomic) IBOutlet UIButton *statusBtn;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *connectStatusLabel;
@property (weak, nonatomic) IBOutlet UILabel *connectFailLabel;
@property (weak, nonatomic) IBOutlet UIButton *scanAgainBtn;
@property (weak, nonatomic) IBOutlet UIButton *helpBtn;
@property (weak, nonatomic) IBOutlet UILabel *failTipsLabel;

@property (nonatomic, strong) CBPeripheral *peripheral;

@end

@implementation DeviceConnectCheckView
{
    BlutoothModel *model;
}

+ (void)show:(NSString *)mac sn:(NSString *)sn type:(NSInteger)type peripheral:(CBPeripheral *)peripheral {
    DeviceConnectCheckView *view = (DeviceConnectCheckView *)[[[NSBundle mainBundle] loadNibNamed:@"DeviceConnectCheckView" owner:self options:nil] firstObject];
    view.frame = [[UIScreen mainScreen] bounds];
    view.mac = mac;
    view.sn = sn;
    view.type = type;
    view.descLabel.hidden = type == 0;
    view.peripheral = peripheral;
    view.titleLabel.text = NSLocalizedString(@"Search via Bluetooth", nil);
    view.connectStatusLabel.text = NSLocalizedString(@"Connecting..", nil);
    view.descLabel.text = NSLocalizedString(@"Make sure the Bluetooth device is turned on and as   to the phone as possible", nil);
    view.statusLabel.text = NSLocalizedString(@"Device connection successful", nil);
    [view.statusBtn setTitle:NSLocalizedString(@"View device", nil) forState:UIControlStateNormal];
    view.connectFailLabel.text = NSLocalizedString(@"Bluetooth connection failed", nil);
    [view.scanAgainBtn setTitle:NSLocalizedString(@"Scan again", nil) forState:UIControlStateNormal];
    [view.helpBtn setTitle:NSLocalizedString(@"Need Help", nil) forState:UIControlStateNormal];
    view.failTipsLabel.text = NSLocalizedString(@"The specified Bluetooth device was not found, please confirm whether the Bluetooth device is turned on or tryto get close to the Bluetooth device", nil);

    NSString *name = [[BTManager sharedBTManager].diviceNameDict objectForKey:view.peripheral.name];
    name = name.length>0 ? name : view.peripheral.name;
    view.bleNameLabel.text = name;
    [[UIApplication sharedApplication].delegate.window addSubview:view];
    [[BTManager sharedBTManager] connectPeripheral:view.peripheral];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectSuccess) name:kBTConnectionSucceed object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(connectFail) name:kBTFailToConnect object:nil];
}

- (IBAction)actionClose:(id)sender {
    [self removeFromSuperview];
    if ([BTManager sharedBTManager].peripheral) {
        [[BTManager sharedBTManager] disconnectRootPeripheral];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddSuccessViewDetail" object:nil];
}

- (IBAction)actionView:(id)sender {
    [self removeFromSuperview];
    if ([BTManager sharedBTManager].peripheral) {
        [[BTManager sharedBTManager] disconnectRootPeripheral];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddSuccessViewDetail" object:model];
}

- (IBAction)actionScan:(id)sender {
    [self removeFromSuperview];
    if ([BTManager sharedBTManager].peripheral) {
        [[BTManager sharedBTManager] disconnectRootPeripheral];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AddRestartView" object:nil];
}

- (IBAction)actionHelp:(id)sender {
//    HelpCenterViewController *vc = [[HelpCenterViewController alloc] initWithNibName:@"HelpCenterViewController" bundle:nil];
//    [self.navigationController pushViewController:vc animated:YES];
}

- (void)connectSuccess {
    [ProgressHUD hideHUD];
    self.isConnected = YES;
    [self checkConnect];
    
    NSString *listStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"BatteryBluList"] ?: @"";
    NSMutableArray *arr = [NSMutableArray array];
    if (listStr.length) {
        NSArray *dataArray = [BlutoothModel covertToModelList:listStr];
        [arr addObjectsFromArray:dataArray];
    }
    model = [[BlutoothModel alloc] init];
    model.name = self.peripheral.name;
    model.uuid = self.peripheral.identifier.UUIDString;
    model.mac = self.mac;
    model.sn = self.sn;
    [arr addObject:model];
    [[NSUserDefaults standardUserDefaults] setObject:[BlutoothModel toJson:arr] forKey:@"BatteryBluList"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BatteryBluListDidChanged" object:nil];
}

- (void)connectFail {
    [ProgressHUD hideHUD];
    self.isConnected = NO;
    [self checkConnect];
}

- (void)checkConnect {
    self.connectView.hidden = YES;
    if (self.type == 1 && !self.isConnected) {
        self.contentH.constant = 475;
        self.scanErrorView.hidden = NO;
    }else {
        self.contentH.constant = 354;
        self.resultView.hidden = NO;
        if (!self.isConnected) {
            self.statusLabel.text = NSLocalizedString(@"Device connection fail", nil);
            self.statusIV.image = [UIImage imageNamed:@"add_fail"];
            self.statusBtn.hidden = YES;
        }
    }
}


@end
